package com.hzxy.yiyid.web.tool;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * @ClassName PicTool
 * @Description 图片工具包
 * @Author RAICOM2023
 * @Date 2023/5/1 16:41
 * @Version 1.0
 **/
public class PicTool {

    private static final Logger logger = LoggerFactory.getLogger(PicTool.class);

    public enum SpecificationHints {
        PIXEL_TOO_SMALL,
        PIXEL_TOO_LARGE,
        FILE_TOO_SMALL,
        FILE_TOO_LARGE,
        NOT_SUIT,
        SUIT
    }

    public static SpecificationHints checkSpecificationFromPicByte(byte[] picByteArr) {
        if (picByteArr.length < 128) {
            return SpecificationHints.FILE_TOO_SMALL;
        }
        if (picByteArr.length > 10 * 1024 * 1024) {
            return SpecificationHints.FILE_TOO_LARGE;
        }

        try (InputStream inputStream = new ByteArrayInputStream(picByteArr)) {
            BufferedImage bufferedImage = ImageIO.read(inputStream);
            if (bufferedImage == null) {
                return SpecificationHints.NOT_SUIT;
            }
            int width = bufferedImage.getWidth();
            int height = bufferedImage.getHeight();
            if (width < 256 || height < 256) {
                return SpecificationHints.PIXEL_TOO_SMALL;
            }
            if (width * height >= 1920*1080) {
                return SpecificationHints.PIXEL_TOO_LARGE;
            }
        } catch (Exception e) {
            logger.error("check pic specification error", e);
            return SpecificationHints.NOT_SUIT;
        }
        return SpecificationHints.SUIT;
    }

}
